/************************************************************************************/
/*  Program name CAPFNDEXT_DOC_SEQ.sql                                              */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;
SET VERIFY OFF

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDEXT_DOC_SEQ';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName1	VARCHAR2(100)	:=	'CAPFND_DOC_SEQ.dat';
	lv_FileName2	VARCHAR2(100)	:=	'CAPFND_DOC_SEQA.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO			VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_F(p_NGO VARCHAR2) IS
	SELECT	*
	FROM	FND_DOCUMENT_SEQUENCES
	WHERE	Name			LIKE	p_NGO||'%';

	CURSOR Cur_G(p_NGO VARCHAR2) IS
	SELECT
		B.*
	FROM
		FND_DOCUMENT_SEQUENCES			A,
		FND_DOC_SEQUENCE_ASSIGNMENTS	B
	WHERE	A.Doc_Sequence_ID	=	B.Doc_Sequence_ID
	AND		UPPER(A.Name)	LIKE	p_NGO||'%';

BEGIN
	lv_NGO			:=	UPPER('&1');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO;

	IF	lv_NGO_Count	>=	1	THEN
		lv_Scc_Count	:=	0;
		lv_Err_Msg		:=	'No Error';
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName1, 'w');
		FOR C1 IN Cur_F(lv_NGO) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.Doc_Sequence_ID||CHR(9)||
				C1.Name||CHR(9)||
				C1.Last_Update_Date||CHR(9)||
					C1.Last_Updated_By||CHR(9)||
				C1.Creation_Date||CHR(9)||
					C1.Created_By||CHR(9)||
				C1.Last_Update_Login||CHR(9)||
				C1.Application_ID||CHR(9)||
				C1.Audit_Table_Name||CHR(9)||
				C1.Message_Flag||CHR(9)||
				C1.Start_Date||CHR(9)||
				C1.Type||CHR(9)||
				C1.DB_Sequence_Name||CHR(9)||
				C1.End_Date||CHR(9)||
				C1.Initial_Value||CHR(9)||
				C1.Table_Name||CHR(9)||
				C1.Attribute_Category||CHR(9)||
				C1.Attribute1||CHR(9)||
				C1.Attribute2||CHR(9)||
				C1.Attribute3||CHR(9)||
				C1.Attribute4||CHR(9)||
				C1.Attribute5||CHR(9)||
				C1.Attribute6||CHR(9)||
				C1.Attribute7||CHR(9)||
				C1.Attribute8||CHR(9)||
				C1.Attribute9||CHR(9)||
				C1.Attribute10||CHR(9)||
				C1.Attribute11||CHR(9)||
				C1.Attribute12||CHR(9)||
				C1.Attribute13||CHR(9)||
				C1.Attribute14||CHR(9)||
				C1.Attribute15||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C1.Created_By)
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg	:=	lv_NGO||': No Doc Seq for this NGO';
		END IF;
		UTL_FILE.FClose(lv_UTL_File);
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID||' CAP_DOC_SEQ',
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO||': No such NGO exists';
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID,
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
	END IF;

	lv_Err_Count	:=	0;

	IF	lv_NGO_Count	>=	1	THEN
		lv_Scc_Count	:=	0;
		lv_Err_Msg		:=	'No Error';
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName2, 'w');
		FOR C2 IN Cur_G(lv_NGO) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C2.DOC_SEQUENCE_ASSIGNMENT_ID||CHR(9)||
				C2.LAST_UPDATE_DATE||CHR(9)||
					C2.LAST_UPDATED_BY||CHR(9)||
				C2.CREATION_DATE||CHR(9)||
					C2.CREATED_BY||CHR(9)||
				C2.LAST_UPDATE_LOGIN||CHR(9)||
				C2.APPLICATION_ID||CHR(9)||
					C2.DOC_SEQUENCE_ID||CHR(9)||
				C2.CATEGORY_CODE||CHR(9)||
				C2.START_DATE||CHR(9)||
				C2.END_DATE||CHR(9)||
					C2.SET_OF_BOOKS_ID||CHR(9)||
				C2.METHOD_CODE||CHR(9)||
				C2.ATTRIBUTE_CATEGORY_DEVELOPER||CHR(9)||
				C2.ATTRIBUTE_CATEGORY||CHR(9)||
				C2.ATTRIBUTE1||CHR(9)||
				C2.ATTRIBUTE2||CHR(9)||
				C2.ATTRIBUTE3||CHR(9)||
				C2.ATTRIBUTE4||CHR(9)||
				C2.ATTRIBUTE5||CHR(9)||
				C2.ATTRIBUTE6||CHR(9)||
				C2.ATTRIBUTE7||CHR(9)||
				C2.ATTRIBUTE8||CHR(9)||
				C2.ATTRIBUTE9||CHR(9)||
				C2.ATTRIBUTE10||CHR(9)||
				CAPBREXT_COMMON.GetUser(C2.Last_Updated_By)||CHR(9)||
				CAPBREXT_COMMON.GetUser(C2.Created_By)||CHR(9)||
				CAPBREXT_COMMON.GetDocSeqName(C2.Doc_Sequence_ID)||CHR(9)||
				CAPBREXT_COMMON.GetSOBShortName(C2.SET_OF_BOOKS_ID)
			);
			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		IF	lv_Scc_Count	=	0	THEN
			lv_Err_Msg	:=	lv_NGO||': No Doc Seq Assign for this NGO';
		END IF;
		UTL_FILE.FClose(lv_UTL_File);
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID||' CAP_DOC_SEQA',
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO||': No such NGO exists';
		INSERT INTO CAPCNV_LOG(
			PROG_ID,
			PROCESS_DATE,
			RECORD_PROCESSED,
			RECORD_REJECTED,
			RECORD_EXTRACTED,
			ERR_MSG
		) VALUES (
			lv_Prog_ID,
			SYSDATE,
			lv_Scc_Count,
			lv_Err_Count,
			lv_Scc_Count-lv_Err_Count,
			lv_Err_Msg
		);
	END IF;
	COMMIT;
END;
/